﻿using SkiaSharp;

namespace fabao.Common.Util
{
    public class ImageHelper
    {
        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="imageStream">原始图片文件流</param>
        /// <param name="maxSizeInBytes">最大图片大小</param>
        /// <param name="maxWidth">最大宽度，以像素为单位</param>
        /// <param name="maxHeight">最大高度，以像素为单位</param>
        /// <returns>返回是否经过处理</returns>
        public static (bool, Stream) CompressImage(Stream imageStream, long maxSizeInBytes, int maxWidth = 8192, int maxHeight = 8192)
        {
            long length = imageStream.Length;
            imageStream.Position = 0;
            using (var originalImage = SKBitmap.Decode(imageStream))
            {
                if (length <= maxSizeInBytes && originalImage.Height <= maxHeight && originalImage.Width <= maxWidth)
                {
                    return (false, imageStream);
                }

                int newWidth = originalImage.Width;
                int newHeight = originalImage.Height;
                float scale = 0;
                if (originalImage.Width > maxWidth || originalImage.Height > maxHeight)
                {
                    if (originalImage.Width > originalImage.Height)
                    {
                        newWidth = maxWidth;
                        scale = (float)maxWidth / originalImage.Width;
                        newHeight = (int)(originalImage.Height * scale);
                    }
                    else
                    {
                        newHeight = maxHeight;
                        scale = (float)maxHeight / originalImage.Height;
                        newWidth = (int)(originalImage.Width * scale);
                    }
                }

                using (var resizedImage = new SKBitmap(new SKImageInfo(newWidth, newHeight)))
                {
                    originalImage.ScalePixels(resizedImage, SKFilterQuality.High); // 使用高品质滤镜

                    int compressionQuality = 100;
                    length = 0;
                    Stream s = Stream.Null;
                    while (length == 0 || length > maxSizeInBytes)
                    {
                        compressionQuality -= 10;
                        s = new MemoryStream();
                        using (var data = resizedImage.Encode(SKEncodedImageFormat.Jpeg, compressionQuality))
                        {
                            data.SaveTo(s);
                        }
                        length = s.Length;
                    }

                    return (true, s);
                }
            }
        }

        /// <summary>
        /// 获取图片文件流
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        public static Stream DownloadImg(string imagePath)
        {
            using (var client = new HttpClient())
            {
                var s = new MemoryStream();
                var response = client.GetAsync(imagePath).GetAwaiter().GetResult();
                if (response.IsSuccessStatusCode)
                {
                    response.Content.CopyToAsync(s).GetAwaiter().GetResult();
                }
                return s;
            }
        }
    }
}
